﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.Entity;
using HRM.BIZ;
using HRM.Utility;

namespace WebUI.Pages
{
    public partial class EmployeeLeaveManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                LoadData();
            }
        }

        protected void InitControl()
        {
            string id = Request.QueryString["id"];
            Employee emp = new EmployeeBiz().GetByID(id.ToInt32());
            if (emp != null)
            {
                ucEmpNavigate.RecordID = emp.ID;
                ucEmpNavigate.EmployeeName = emp.Name;
            }

            LeaveTypeBiz leaveBiz = new LeaveTypeBiz();
            var result = leaveBiz.GetAll();
            ddlLeaveType.DataSource = result;
            ddlLeaveType.DataValueField = "ID";
            ddlLeaveType.DataTextField = "Name";
            ddlLeaveType.DataBind();
        }

        protected void LoadData()
        {
            string id = Request.QueryString["id"];
            EmployeeLeaveBiz empBiz = new EmployeeLeaveBiz();
            var result = empBiz.GetEmployeeLeaves(id.ToInt32());
            grvEmployeeLeave.DataSource = result;
            grvEmployeeLeave.DataBind();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            btnAction.CommandName = "Add";
            btnAction.CommandArgument = "";
            btnAction.Text = "Thêm";
            txtNumOfDay.Text = "";
            txtFrom.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtTo.Text = DateTime.Now.ToString("dd/MM/yyyy");

            divAction.Style.Add("display", "block");
        }

        protected void grvEmployeeLeave_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chk = e.Row.FindControl("chkAll") as CheckBox;
                if (chk != null)
                {
                    chk.Attributes.Add("onclick", string.Format("selectAll('{0}')", chk.ClientID));
                }
            }
        }

        protected void grvEmployeeLeave_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkAll = new CheckBox();
                chkAll.ID = "chkAll";
                e.Row.Cells[0].Controls.Add(chkAll);
            }
        }

        protected void grvEmployeeLeave_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditItem":
                    {
                        EmployeeLeaveBiz empBiz = new EmployeeLeaveBiz();
                        var item = empBiz.GetByID(e.CommandArgument.ToInt32());
                        if (item != null)
                        {
                            btnAction.CommandName = "Edit";
                            btnAction.CommandArgument = e.CommandArgument.ToString();
                            btnAction.Text = "Lưu";

                            txtFrom.Text = item.From.ToString("dd/MM/yyyy");
                            txtTo.Text = item.To.ToString("dd/MM/yyyy");
                            txtNumOfDay.Text = item.NumOfDays.ToString();
                            ddlLeaveType.SelectedValue = item.LeaveTypeID.ToString();
                            divAction.Style.Add("display", "block");
                        }
                        break;
                    }
                case "DeleteItem":
                    {
                        EmployeeLeaveBiz empBiz = new EmployeeLeaveBiz();
                        empBiz.Delete(e.CommandArgument.ToInt32());
                        LoadData();
                        break;
                    }
            }
        }

        protected void grvEmployeeLeave_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvEmployeeLeave.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            switch (btnAction.CommandName)
            {
                case "Add":
                    {
                        EmployeeLeaveBiz empBiz = new EmployeeLeaveBiz();
                        EmployeeLeave item = new EmployeeLeave();
                        item.From = txtFrom.Text.ToDateTime("dd/MM/yyyy");
                        item.To = txtTo.Text.ToDateTime("dd/MM/yyyy");
                        item.NumOfDays = txtNumOfDay.Text.ToFloat();
                        item.LeaveTypeID = ddlLeaveType.SelectedValue.ToInt32();
                        item.EmpID = Request.QueryString["id"].ToInt32();

                        empBiz.Add(item);
                        (Master as DefaultMasterPage).RunStartupScript("displayMessage('Thêm thành công')");
                        divAction.Style["display"] = "none";
                        LoadData();
                        break;
                    }
                case "Edit":
                    {
                        EmployeeLeaveBiz empBiz = new EmployeeLeaveBiz();
                        var item = empBiz.GetByID(btnAction.CommandArgument.ToInt32());
                        if (item != null)
                        {
                            item.From = txtFrom.Text.ToDateTime("dd/MM/yyyy");
                            item.To = txtTo.Text.ToDateTime("dd/MM/yyyy");
                            item.NumOfDays = txtNumOfDay.Text.ToFloat();
                            item.LeaveTypeID = ddlLeaveType.SelectedValue.ToInt32();

                            empBiz.Update(item);
                            (Master as DefaultMasterPage).RunStartupScript("displayMessage('Cập nhật thành công')");
                            divAction.Style["display"] = "none";
                            LoadData();

                        }
                        break;
                    }
            }
        }
    }
}
